/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.com.bolsaTrabajo.model.dao;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import sv.com.bolsaTrabajo.model.entities.Departamento;
import sv.com.bolsaTrabajo.model.entities.Municipio;

/**
 *
 * @author sysmanager
 */
@Stateless
public class MunicipioFacade extends AbstractFacade<Municipio> {

    @PersistenceContext(unitName = "BolsaTrabajoPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public MunicipioFacade() {
        super(Municipio.class);
    }

    @SuppressWarnings("null")
    public Integer obtenerNuevoIdentificador() throws Exception {
        try {
            Query q = getEntityManager().createNativeQuery("select (max(o.id_municipio)+1) from municipio o");
            Object rres = (Object) q.getSingleResult();
            Long res = Long.parseLong(rres.toString());
            if (res == null) {
                return 1;
            }
            return Integer.parseInt(res.toString());
        } catch (NullPointerException ne) {
            return 1;
        } catch (NumberFormatException e) {
            throw e;
        }
    }

    public List<Municipio> obtenerTodosMunicipioPorDepar(Departamento id_departamento) {
        Query q = getEntityManager().createQuery("select m from Municipio m where m.id_departamento = :idDepartamento");
        q.setParameter("Departamento", id_departamento);
        return q.getResultList();
    }
}
